#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <functional>

using std::cout;
using std::endl;
using std::remove_if;
using std::copy;
using std::for_each;
using std::vector;
using std::ostream_iterator;
using std::bind1st;
using std::bind2nd;

bool func(int value)
{
    return value > 5;
}

void print(int value)
{
    cout << value << "  ";
}

void test()
{
    vector<int> vec = {1, 3, 5, 7, 5, 8, 9, 2};
    copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "  "));
    cout << endl;

    cout << endl << "测试remove_if函数" << endl;
    //remove_if不会将真正的元素删除，但是会返回待删除的元素的
    //迭代器，然后配合erase函数进行删除
    /* auto it = remove_if(vec.begin(), vec.end(), func); */
    /* vec.erase(it, vec.end()); */
    /* for_each(vec.begin(), vec.end(), print); */
    /* cout << endl; */

    //lambda表达式
    /* auto it = remove_if(vec.begin(), vec.end(), [](int value)->bool{ */
    /*                     return value > 5; */
    /*                     }); */
    /* vec.erase(it, vec.end()); */
    /* for_each(vec.begin(), vec.end(), print); */
    /* cout << endl; */

    //删除的是小于5的
    /* auto it = remove_if(vec.begin(), vec.end(), */ 
    /*                     bind1st(std::greater<int>(), 5)); */

    //删除的是大于5的
    auto it = remove_if(vec.begin(), vec.end(), 
                        bind1st(std::less<int>(), 5));

    //删除的是大于5的
    /* auto it = remove_if(vec.begin(), vec.end(), */ 
    /*                     bind2nd(std::greater<int>(), 5)); */
    vec.erase(it, vec.end());
    for_each(vec.begin(), vec.end(), print);
    cout << endl;
}

void test2()
{
    vector<int> vec;
    vec.push_back(1);

    bool flag = true;
    for(auto it = vec.begin(); it != vec.end(); ++it)
    {
        cout << *it << "  ";//读
        if(flag)
        {
            //底层已经发生了扩容,迭代器失效了
            vec.push_back(2);//写
            flag = false;
            it = vec.begin();//重置迭代器的位置
        }
    }
    cout << endl;
}

int main(int argc, char **argv)
{
    test();
    return 0;
}

